export default {

	//页面跳转
	toPath(url) {
		uni.navigateTo({
			url: url
		})
	},
	//页面跳转销毁
	toPaths(url) {
		uni.reLaunch({
			url: url
		})
	},
	//底部导航跳转
	toTable(url) {
		uni.switchTab({
			url: url
		})
	},
	//返回
	toBack(val = 1) {
		uni.navigateBack({
			delta: val
		})
	},
	changeTime(timestamp = '', type = 1, state = 1) {
		if (timestamp.toString().length <= 10) {
			timestamp = timestamp * 1000;
		}
		var date = new Date();
		if (timestamp) var date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000

		let Y = date.getFullYear() + '-';

		let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';

		let D = date.getDate() + ' ';

		if (type > 3) {

			if (state == 1) {
				return (Y + M).replace(/-/g, '/');
			} else {
				let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
				return (Y + M);
			}
		}

		if (type > 2) {
			let h = date.getHours() + ':';
			let m = date.getMinutes();
			if (state == 1) {
				return (Y + M + D + h + m).replace(/-/g, '/');
			} else {
				return (Y + M + D + h + m);
			}

		}
		if (type > 1) {
			let h = date.getHours() + ':';

			let m = date.getMinutes() + ':';

			let s = date.getSeconds();
			if (state == 1) {
				return (Y + M + D + h + m + s).replace(/-/g, '/');
			} else {
				return (Y + M + D + h + m + s);
			}

		}
		if (state == 1) {
			return (Y + M + D).replace(/-/g, '/');
		} else {
			return (Y + M + D);
		}
	},
	//提示
	toast(title) {
		uni.showToast({
			title,
			icon: 'none'
		})
	},
	//距离
	getDistance(lat1, lng1, lat2, lng2, unit = 'km') {
		function Rad(d) {
			//根据经纬度判断距离
			return d * Math.PI / 180.0;
		}
		// lat1用户的纬度
		// lng1用户的经度
		// lat2商家的纬度
		// lng2商家的经度
		var radLat1 = Rad(lat1);
		var radLat2 = Rad(lat2);
		var a = radLat1 - radLat2;
		var b = Rad(lng1) - Rad(lng2);
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(
			Math
			.sin(b / 2), 2)));
		s = s * 6378.137;
		if (unit == 'km') {
			s = Math.round(s * 10000) / 10000;
			s = s.toFixed(1) + 'km' //保留两位小数
			return s
		} else {
			s = Math.round(s * 10000) / 10;
			s = s.toFixed(1) //保留两位小数
			return s
		}
	},
	//判断图片路径是否加域名
	isUrl(url, path) {
		let data = url.split('/');
		if (data[0] == 'https' || data[0] == "http") {
			return url;
		} else {
			return path + url;
		}
	},
	//去除路径中的域名
	delUrl(url) {
		return '/' + url.split('/').slice(3).join('/');
	},
	//打电话
	tel(phone) {
		uni.makePhoneCall({
			phoneNumber: phone + '',
			success(res) {

			},
			fail(error) {

			}
		})
	},
	//数组除去空值
	removeEmpty(arr) {
		for (var i = 0; i < arr.length; i++) {
			if (arr[i] == "" || typeof(arr[i]) == "undefined") {
				arr.splice(i, 1);
				i = i - 1; // i - 1 ,因为空元素在数组下标 2 位置，删除空之后，后面的元素要向前补位
			}
		}
		return arr;
	},
	//值是否存在于数组
	inArray(val, array) {
		for (let i = 0; i < array.length; i++) {

			if (val === array[i]) {

				return true;
			}
		}
		return false;
	},
	//对象的键值是否存在数组
	inObjectArray(val, array, key) {
		for (let i = 0; i < array.length; i++) {
			if (val[key] === array[i][key]) {
				return true;
			}
		}
		return false;
	},
	//微信打开文件
	wxOpenFile(url, type = 1) {

		uni.downloadFile({
			url: url,
			success: function(res) {
				if (res.statusCode === 200) {
					var path = res.tempFilePath;
					if (type == 1) {
						uni.openDocument({
							showMenu: true,
							filePath: path,
							success: function(res) {

							}
						})
					}
					if (type != 1) {
						uni.getFileSystemManager().saveFile({
							tempFilePath: path,
							success: function(data) {
								console.log(data);
								uni.showToast({
									icon: 'none',
									mask: true,
									title: '文件已保存!',
									duration: 3000,
								});
							},
							fail: function(res) {

							}
						})
					}

				}
			},
			fail: function(res) {
				uni.showToast({
					title: '操作失败'
				});
			}
		})
	},
	//手机号保留星号
	getMobileSlice(mobile) {
		if (!mobile) return mobile;
		let text = mobile.slice(0, 3) + '****' + mobile.slice(mobile.length - 4);
		return text;
	},
	//时分秒加工
	setTimeGe(item) {
		return (item.startHour + '').padStart(2, 0) + ':' + (item.startMinute + '').padStart(2, 0) + '-' + (item
			.endHour + '').padStart(2, 0) + ':' + (item.endMinute + '').padStart(2, 0);
	},
	//价格公式
	setPriceGe(item, type = 1) {
		if (!item) return;
		switch (item.chargingMethod) {
			case 'hour':
				if (type == 1) {
					return '(' + item.price + '元/时)';
				} else {
					return item.price + '元/时';
				}

				break;
			case 'sequence':
				if (type == 1) {
					return '(' + item.price + '元/次)';
				} else {
					return item.price + '元/次';
				}

				break;
		}
	},
	//时间分割
	setTime(time, type = 1) {

		time = time.split(' ');
		if (type == 1) {
			let times = time[0].split('-');
			return times[0] + '年' + times[1] + '月' + times[2] + '日';
		}
		if (type == 2) {
			return time[1];
		}
		if(type==3){
			let times = time[0].split('-');
			return times[0] + '-' + times[1] + '-' + times[2];
		}
	},
	//状态
	setState(state) {
		let text = '';
		if (state) {
			text = '已完成';
		} else {
			text = '使用中'
		}
		return text;
	},
	//被子状态
	setQuiltState(state) {
		let text = '';
		if (state) {
			text = '已归还';
		} else {
			text = '使用中'
		}
		return text;
	},
	setQuiltText(state) {
		let text = '';
		if (state) {
			text = '交易成功';
		} else {
			text = '使用完请归还'
		}
		return text;
	},
	//富文本图片
	formatRichText(html) {
		let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
			return match;
		});
		newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
			match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
				'max-width:100%;');
			return match;
		});
		newContent = newContent.replace(/<br[^>]*\/>/gi, '');
		newContent = newContent.replace(/\<img/gi,
			'<img style="width:100%;height:auto;object-fit:fill;padding:0;margin:0;display:flex;"');
		return newContent;
	},
	getDhm(start, end) {
		// 时间相差秒数
		let dateDiff = end.getTime() - start.getTime();

		// 计算出相差天数
		let days = Math.floor(dateDiff / (24 * 3600 * 1000));

		// 计算出小时数
		let residue1 = dateDiff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
		let hours = Math.floor(residue1 / (3600 * 1000));

		// 计算相差分钟数
		let residue2 = residue1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
		let minutes = Math.floor(residue2 / (60 * 1000));

		// 计算相差秒数
		let residue3 = residue2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
		let seconds = Math.round(residue3 / 1000);

		let fenText = '分';
		if (days == 0 && hours == 0) {
			fenText = '分钟';
		}

		let returnVal =
			((days == 0) ? "" : days + "天") +
			((hours == 0) ? "" : hours + "时") +
			((minutes == 0) ? "" : minutes + fenText);

		if (days == 0 && hours == 0 && minutes == 0) {
			returnVal = ((days == 0) ? "" : days + "天") +
				((hours == 0) ? "" : hours + "时") +
				((minutes == 0) ? "" : minutes + fenText) +
				((seconds == 0) ? "" : seconds + "秒");
		}
		return returnVal;
	},
	toPhone() {

		uni.showModal({
			title: '温馨提示',
			content: '客服工作时间:8:00–20:00',
			showCancel: true,
			confirmText: '拨打电话',
			success(ress) {
				if (ress.confirm) {
					uni.makePhoneCall({
						phoneNumber: '400-685-9020',
						success(res) {
							console.log(res);
						},
						error(error){
							console.log(error);
						}
					})
				}
			}
		});
	},
	//验证手机号格式
	isMobile(val) {
		var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
		return myreg.test(val);
	},
	//是否开启定位授权
	isLocation() {
		uni.getSetting({
			success: (res) => {
				if (res.authSetting['scope.userLocation'] === true) {
					return true;
				}
				uni.showModal({
					title: '提示',
					content: '请开启位置授权,否则无法使用',
					showCancel: false,
					confirmText: '去开启',
					success(ress) {
						if (ress.confirm) {
							uni.openSetting({
								success: (data) => {
									if (data.authSetting['scope.userLocation'] ===
										true) {
										return true;
									} else {
										uni.showModal({
											title: '提示',
											content: '请开启位置授权,否则无法使用',
											showCancel: false,
											success() {
												return false;
											}
										})
									}
								}
							})
						}
					}
				})

			}
		})
	},
	getNetwork(){
		uni.getNetworkType({
		  success: function (res) {
		   
		    if (res.networkType === 'none') {
		    uni.showModal({
		    	title: '温馨提示',
		    	content: '当前无网络连接,请检查网络',
		    	showCancel: false
		    });
		      // 处理无网络的情况
		    } 
		  }
		});
	},
	getMacMsg(mac,macList){
	  var msg='要查找的设备:'+mac+';';
	  var arr=[];
	  console.log(macList);
	  arr=Object.keys(macList);
		 
	  if(arr&&arr.length>0){
		   msg+='目前搜索到的设备:'+(arr.join(','));
	  }else{
		  msg+='目前搜索到的设备:无'
	  }
	  console.log(arr,msg)
	 return msg;
	}
}